package cn.edu.flink.tutorial.wordcount.socket;


import java.io.*;
import java.net.*;

public class WordCountServerSocket {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 创建服务端socket
        ServerSocket serverSocket = new ServerSocket(8088);

        while (true) {
            // 监听客户端
            Socket socket = serverSocket.accept();

            InetAddress address = socket.getInetAddress();
            System.out.println("当前客户端的IP：" + address.getHostAddress());

            OutputStream outputStream = socket.getOutputStream();
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream));

            while (true) {
                printWriter.write("thinking");
                printWriter.write(" ");
                printWriter.write("machine");
                printWriter.write("\n");
                printWriter.flush();
                Thread.sleep(2000);
                System.out.print("+");
                try {
                    socket.sendUrgentData(0xFF);
                } catch (Exception e) {
                    System.out.println("连接断开！");
                    break;
                }
            }
        }

    }
}
